<template>
  <div class="big-box">
    <div class="search-container">
      <el-form
        :inline="true"
        label-width="100px"
        class="workId"
        ref="queryFormRef"
        :model="queryParams"
      >
        <el-form-item label="作文号" prop="workId">
          <el-input
            v-model="queryParams.workId"
            @keyup.enter="handleQuery"
            placeholder="作文号"
            clearable
            style="width: 200px; margin-right: 20px"
        /></el-form-item>
        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="openDialog()"
          ><i-ep-plus />新增作文题</el-button
        >
      </template>
      <el-table
        v-loading="loading"
        ref="teacherFormRef"
        :data="teacherList"
        highlight-current-row
        border
      >
        <el-table-column
          label="作文号"
          prop="workId"
          width="200"
          align="center"
        />
        <el-table-column label="作文题目" align="center" prop="tile" />
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 新增弹窗 -->
    <el-dialog
      width="500px"
      @close="closeDialogs"
      title="新增作文题目"
      v-model="dialog.visible"
    >
      <el-form
        label-width="100px"
        ref="teacherFormRef"
        :model="formData"
        :rules="rules"
      >
        <el-form-item label="作文题目" prop="tile">
          <el-input
            placeholder="请输入作文题目"
            type="textarea"
            v-model="formData.tile"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">提 交</el-button>
          <el-button @click="closeDialogs">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import {
  getteacherComPage,
  getteacherComMenuIds,
  addteacherCom,
  updateteacherCom,
  getUserIds,
} from "@/api/composition/teacher/index";
import {
  teacherComForms,
  teacherComQuery,
  teacherComPageVO,
  teacherComPageResult,
  UserPageVO,
} from "@/api/composition/teacher/types";

const value = ref();
// const channelList = ref([]);
const editDialog = ref();
const removeIds = ref([]); // 删除用户ID集合 用于批量删除
// const removeId = ref([]); // 删除用户ID集合 用于批量删除
const loading = ref(false);
const teacherList = ref<teacherComPageVO[]>();
const userList = ref<UserPageVO[]>();
const total = ref(0);
const queryFormRef = ref(ElForm);
const queryParams = reactive<teacherComQuery>({
  workId: "",
  page: 1,
  pageSize: 10,
});
const UserPageVOs = reactive<UserPageVO>({});

//验证
const rules = reactive({
  tile: [
    {
      required: true,
      message: "请填写题目",
      trigger: "blur",
    },
  ],
  username: [{ required: true, message: "用户名不在", trigger: "blur" }],
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getteacherComPage(queryParams)
    .then(({ data }) => {
      teacherList.value = data.items;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.page = 1;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});

const teacherFormRef = ref(ElForm);

const formData = reactive<teacherComForms>({
  id: "",
  username: "",
  workId: "",
  tile: "",
});
/** 关闭新增修改表单弹窗 */
function closeDialogs() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  teacherFormRef.value.resetFields();
  teacherFormRef.value.clearValidate();

  formData.id = "";
  formData.username = "";
  formData.workId = "";
  formData.tile = "";
}

const dialog = reactive({
  title: "",
  visible: false,
});

/** 打开修改增加表单弹窗 */
function openDialog() {
  dialog.visible = true;
  // // console.log(id.id);
  // console.log(workId.workId);
  // // const ID = id.id;
  // const ID = workId.workId;
  // dialog.title = "写作";
}

import { useUserStore } from "@/store/modules/user";
const { user } = useUserStore();
console.log(user);

/** 新增修改保存提交 */
function handleSubmit() {
  // teacherFormRef.value.validate((valid: any) => {
  //   if (valid) {

  //   }
  // });
  ElMessageBox.confirm("确认增加?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    const username = user.username;
    formData.username = username;
    addteacherCom(formData)
      .then(() => {
        ElMessage.success("新增成功");
        closeDialogs();
        resetQuery();
      })
      .finally(() => (loading.value = false));
  });
}
</script>

<style scoped lang="scss">
.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}
</style>
